package com.cah.game;

import com.cah.library.DatabaseHelper;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;

public class LoadScreenActivity extends Activity
{
	String gameKey;
	
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.loadscreen);
		
		Intent intent = getIntent();
		if(intent.getStringExtra("gameKey") != null)
		{
			gameKey = intent.getStringExtra("gameKey");
			Log.e("gameKey", gameKey);
		}
		
		new RedirectProcess().execute();
		
	}
	
	private class RedirectProcess extends AsyncTask<Void, Void, String>
	{

		@Override
		protected String doInBackground(Void... params)
		{
			DatabaseHelper dbhelper = new DatabaseHelper(getApplicationContext());
			String userKey = dbhelper.getUserKey();
			return userKey;
		}
		
		protected void onPostExecute(String userKey)
		{
			if(userKey.length()==0)
			{
				Intent newIntent = new Intent(getApplicationContext(), LoginActivity.class);
				startActivity(newIntent);
				finish();
			}
			else
			{
				Intent newIntent = new Intent(getApplicationContext(), MainActivity.class);
				newIntent.putExtra("userKey", userKey);
				if(gameKey != null)
				{
					newIntent.putExtra("startGameFrag", true);
					newIntent.putExtra("gameKey", gameKey);
				}
				startActivity(newIntent);
				finish();
			}
		}
		
	}
}
